﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ZR.Infrastructure.ANet
{
    public class Base64Util
    {
        /// <summary>
        /// Base64加密，采用utf8编码方式加密
        /// </summary>
        /// <param name="source">待加密的明文</param>
        /// <returns>加密后的字符串</returns>
        public static string Base64Encode(string source)
        {
            return Base64Encode(Encoding.UTF8, source);
        }

      

        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="encodeType">加密采用的编码方式</param>
        /// <param name="source">待加密的明文</param>
        /// <returns></returns>
        public static string Base64Encode(Encoding encodeType, string source)
        {
            string encode = string.Empty;
            byte[] bytes = encodeType.GetBytes(source);
            try
            {
                encode = Base64Encode(bytes);
                //encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = source;
            }
            return encode;
        }

        /// <summary>
        /// 对数组进行base64编码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Base64Encode(byte[] s)
        {
            string result = string.Empty;
            if (s != null)
            {
                try
                {
                    result = Convert.ToBase64String(s);
                    // result = Base64UrlEncoder.Encode(s);
                }
                catch (Exception)
                {
                }
            }
            return result;
        }

        /// <summary>
        /// Base64解密，采用utf8编码方式解密
        /// </summary>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string Base64Decode(string result)
        {
            return Base64Decode(Encoding.UTF8, result);
        }

        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="encodeType">解密采用的编码方式，注意和加密时采用的方式一致</param>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string Base64Decode(Encoding encodeType, string result)
        {
            string decode = string.Empty;
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                decode = encodeType.GetString(bytes);
            }
            catch
            {
                decode = result;
            }

            return decode;
        }

        /// <summary>
        /// base64解码字符串
        /// </summary>
        /// <param name="s">base64编码的字符串</param>
        /// <returns></returns>
        public static byte[] DecodeBuffer(string s)
        {
            byte[] result = null;
            if(!string.IsNullOrEmpty(s))
            {
                result = Convert.FromBase64String(s);
            }
            return result;
        }


        /// <summary>
        /// HMACSHA1签名加密
        /// </summary>
        /// <param name="text">待加密的数据</param>
        /// <param name="key">加密使用的key</param>
        /// <returns></returns>
        public static byte[] HmacSha1Sign(string text, string key)
        {
            Encoding encode = Encoding.UTF8;
            byte[] byteData = encode.GetBytes(text);
            byte[] byteKey = encode.GetBytes(key);
            HMACSHA1 hmac = new HMACSHA1(byteKey);
            CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write);
            cs.Write(byteData, 0, byteData.Length);
            cs.Close();
            return hmac.Hash;

        }

        /// <summary>
        /// MD5加密返回16进制小写
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string source)
        {
            string rule = "";

            MD5 md5 = MD5.Create();

            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(source));

            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得

            for (int i = 0; i < s.Length; i++)

            {

                rule = rule + s[i].ToString("x2"); // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 

            }
            return rule;
        }
    }
}
